<?php
	class Storage_model extends CI_Model {
	    
    var $id;
	var $title;    
	var $total_quantity;
	var $left_quantity;
	var $status;

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    // load all Storage
    function loadAll($cond = false)
    {
        $this->db->select('*');
        $this->db->from('storage');
        if(!empty($cond))
        {
            if(!empty($cond['quantity']))
                $this->db->where('left_quantity > ',$cond['quantity']);
            if(!empty($cond['storage']))
                $this->db->like('title',$cond['storage']);
        }
        
        $query = $this->db->get();

        return $query->result();
    }    

    function getEntry($id)
    {
        $query = $this->db->get_where('storage', array('id' => $id));
        return $query->row();
    }

    function updateQuantity($storage_id,$type)
    {
        $query = $this->db->get_where('storage', array('id' => $storage_id));
        $row = $query->row();

        if($type == 'decrease')
        {
            $quantity = $row->left_quantity - 1;       
        }
        else if($type == 'increase')
        {
            $quantity = $row->left_quantity + 1;       
        }        
        
        $this->db->where('id',$storage_id);
        $this->db->update('storage',array('left_quantity' => $quantity));
    }

    // add function status storage
    function loadStorageStatInfo()
    {
        $data = array();

        // full load
        $fullLoadSql = "SELECT COUNT(id) as id FROM `storage` WHERE `left_quantity` = 0";

        $query = $this->db->query($fullLoadSql);

        $row = $query->row();

        $data['full_load'] = $row->id;

        // available load
        $availableLoadSql = "SELECT COUNT(id) as id FROM `storage` WHERE `left_quantity` > 0 AND `total_quantity` > `left_quantity`";

        $query = $this->db->query($availableLoadSql);

        $row = $query->row();

        $data['available_load'] = $row->id;

        // empty
        $emptyLoadSql = "SELECT COUNT(id) as id FROM `storage` WHERE `total_quantity` = `left_quantity`";

        $query = $this->db->query($emptyLoadSql);

        $row = $query->row();

        $data['empty_load'] = $row->id;

        return $data;
    }

    function getInfo($title)
    {
        $query = $this->db->get_where('storage',array('title'=>$title));
        return $query->row();
    }
}